/**
 *  A model element represents any kind of symbol that appears in a
 *	collaboration diagram. A concrete example of this is an
 *	instance symbol.
 *
 *  @author	K Barclay
 */

package diagram;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public abstract class ModelElement {

	/**
	 * Objet support à la gestion des évènements de sélection.
	 */
	private transient ElementSelectionSupport elementSelectionSupport = new ElementSelectionSupport();

	public void addElementSelectionListener(ElementSelectionListener listener) {
		elementSelectionSupport.addElementSelectedListener(listener);
	}

	public void removeElementSelectionListener(ElementSelectionListener listener) {
		elementSelectionSupport.removeElementSelectedListener(listener);
	}

	protected void fireElementSelectedEvent() {
		ElementSelectionEvent event = new ElementSelectionEvent(this);
		if (isSelected()) {
			elementSelectionSupport.fireElementSelectedEvent(event);
		} else {
			elementSelectionSupport.fireElementDeselectedEvent(event);
		}
	}

	public static final String ELEMENT_CHANGED_EVENT = "ElementChangedEvent";

	public static final String CONTEXTUAL_MENU_EVENT = "ContextualMenuEvent";


	protected PropertyChangeSupport support = new PropertyChangeSupport(this);

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		support.addPropertyChangeListener(ELEMENT_CHANGED_EVENT, listener);
		support.addPropertyChangeListener(CONTEXTUAL_MENU_EVENT, listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		support.removePropertyChangeListener(ELEMENT_CHANGED_EVENT, listener);
		support.removePropertyChangeListener(CONTEXTUAL_MENU_EVENT, listener);
	}

	protected void fireElementChangedEvent() {
		support.firePropertyChange(ELEMENT_CHANGED_EVENT, null, this);
	}

	protected void fireContextualMenuEvent(MouseEvent event) {
		support.firePropertyChange(CONTEXTUAL_MENU_EVENT, null, event);
	}

	public boolean isSelected() {
		return selectedFlag;
	}

	public void setSelected(boolean selected) {
		selectedFlag = selected;
		fireElementSelectedEvent();
	}

	public abstract boolean isHit(int mouseX, int mouseY);

	public boolean isHit(Point mousePoint) {
		return this.isHit(mousePoint.x, mousePoint.y);
	}

	public abstract void draw(Graphics graphicsContext);

	public abstract void doRightMousePressed(MouseEvent event,
			Graphics graphicsContext);

	public abstract void doLeftMousePressed(MouseEvent event,
			Graphics graphicsContext);

	public abstract void doLeftMouseReleased(MouseEvent event,
			Graphics graphicsContext);

	public abstract void doLeftMouseDragged(MouseEvent event,
			Graphics graphicsContext);

	// ---------- properties ----------------------------------

	private boolean selectedFlag;

} // class: ModelElement

// ============================================================================
